package com.tzy.evaluation.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tzy.common.entity.Evaluation;
import com.tzy.evaluation.mapper.EvaluationMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户评价表 服务实现类
 * </p>
 * @author 谭同学
 * @since 2022-03-02
 */
@Service
public class EvaluationService extends ServiceImpl<EvaluationMapper, Evaluation> implements IEvaluationService {
    @Resource
    EvaluationMapper evaluationMapper;

    @Override
    public List<Map<String, Object>> getPendingEvaluationListByUserId(int userId) {
        return evaluationMapper.findPendingEvaluationListByUserId(userId);
    }

    @Cacheable(cacheNames = "page")
    public Page<Evaluation> getEvaluationListByUserId(int userId,Integer pageNum,Integer size) {
        if(pageNum!=null&&size!=null){
            if(pageNum <= 0 ){
                pageNum = 1 * size;
            }else{
                pageNum=(pageNum-1)*size;
            }
        }
        List<Evaluation> data= evaluationMapper.findEvaluationListByUserId(userId,pageNum,size);
        int total = evaluationMapper.findTotal();
        Page<Evaluation> page = new Page<>();
        page.setRecords(data);
        page.setTotal(total);
        return page;
    }

    /**
     * 手写分页方法
     * @param pageNum 当前页数
     * @param size 每页显示的数量
     * @return
     */
    @Cacheable(cacheNames = {"page"})
    public Page<Evaluation> getEvaluationPageByDishesId(int dishesId,Integer pageNum, Integer size){
        if(pageNum!=null&&size!=null){
            if(pageNum <= 0 ){
                pageNum = 1 * size;
            }else{
                pageNum=(pageNum-1)*size;
            }
        }
        List<Evaluation> data= evaluationMapper.findEvaluationPageByDishesId( dishesId,pageNum,size );
        int total = evaluationMapper.findTotal();
        Page<Evaluation> page = new Page<>();
        page.setRecords(data);
        page.setTotal(total);
        return page;
    }
    /**
     * 手写分页方法
     * @param pageNum 当前页数
     * @param size 每页显示的数量
     * @return
     */
    @Cacheable(cacheNames = {"page"})
    public Page<Evaluation> getEvaluationByPage(Integer pageNum, Integer size){
        if(pageNum!=null&&size!=null){
            if(pageNum <= 0 ){
                pageNum = 1 * size;
            }else{
                pageNum=(pageNum-1)*size;
            }
        }
//        List<Evaluation> data= evaluationMapper.findEvaluationByPage( pageNum,size );
        int total = evaluationMapper.findTotal();
        System.out.println("total="+total+",pageNum="+pageNum);
        if( total - (pageNum+1)*size <0 ){
            pageNum = pageNum-1;
        }
        List<Evaluation> records= evaluationMapper.findEvaluationListByPage( pageNum,size );
        Page<Evaluation> page = new Page<>();
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }
    /**
     * 根据 ID 选择修改
     *
     * @param entity 实体对象
     */
    @Override
    @CacheEvict(value = {"page","list"},allEntries = true)
    public boolean updateById(Evaluation entity) {
        return super.updateById(entity);
    }
}
